import java.util.Stack;
//150 - 逆波兰表达式求值
class Solution1 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int result = 0;
        for (int i = 0; i < tokens.length; i++) {
            if(isNumber(tokens[i])){
                int x = Integer.parseInt(tokens[i]);
                stack.push(x);
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (tokens[i]){
                    case "+":
                        result = num1 + num2;
                        break;
                    case "-":
                        result = num1 - num2;
                        break;
                    case "*":
                        result = num1 * num2;
                        break;
                    case "/":
                        result = num1 / num2;
                        break;
                }
                stack.push(result);
            }
        }
        return stack.pop();
    }
    private boolean isNumber(String s){
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return false;
        }
        return true;
    }
}
//20 - 有效的括号
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char x = s.charAt(i);
            if(x == '(' || x == '[' || x == '{'){
                stack.push(x);
            }else if(x == ')' || x == ']' || x == '}'){
                if(!stack.empty()){
                    char tmp = stack.pop();
                    if((tmp == '(' && x == ')')||
                            (tmp == '[' && x == ']') ||
                            (tmp == '{' && x == '}') ){
                    }else {
                        return false;
                    }
                }
                return false;
            }
        }
        return stack.empty();
    }
}
public class Test {
}
